package com.augurit.common.syscode.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.base.extmodel.ExtEditorGridResult;
import com.augurit.common.syscode.convert.SysCodeConverter;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.common.syscode.util.SysCodeUtils;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.util.ExtUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * 数据字典Action类
 */
@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "sys-code.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/common/syscode/sys-code.jsp")
})
public class SysCodeAction extends ExtBaseAction implements ModelDriven<ExtEditorGridResult<SysCodeForm>>, Preparable {

	// Service 引用
	@Autowired
	private ISysCodeService sysCodeService;
	
	// 属性
	private ExtEditorGridResult<SysCodeForm> result;
	private Page<SysCodeForm> page = new Page<SysCodeForm>(15);

	
	/**
	 * 分页获取数据字典类型对象列表
	 */
	public String typeGridList() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		sysCodeService.searchType(page, getFormForSearch());
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 新增数据字典类型对象
	 */
	public String typeGridCreate() throws Exception {
		List<SysCodeForm> list = persistChangeTypeForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridCreateMessage(list), list, false);
		return null;
	}
	
	/**
	 * 更新数据字典类型对象
	 */
	public String typeGridUpdate() throws Exception {
		List<SysCodeForm> list = persistChangeTypeForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridUpdateMessage(list), list, true);
		return null;
	}
	
	/**
	 * 删除数据字典类型对象
	 */
	public String typeGridDelete() throws Exception {
		Long[] ids = result.getDeletedIds();
		List<String> typeCodeList = sysCodeService.deleteType(ids);
		
		//同步全局缓存
		WebContextUtils.syncSysCodeBuffer(typeCodeList, this.getApplication());
		
		this.extRenderEditorGridResult(true, ExtEditorGridResult.getEditorGridDeleteMessage(ids));
		return null;
	}
	
	/**
	 * 保存新增或修改的数据字典类型对象
	 */
	private List<SysCodeForm> persistChangeTypeForms() throws Exception{
		List<SysCodeForm> list = result.getChangedForms(SysCodeForm.class);
		sysCodeService.saveType(list);
		
		//同步全局缓存
		WebContextUtils.syncSysCodeBuffer(SysCodeUtils.convertToTypeCodeList(list), this.getApplication());
		
		return list;
	}
	
	/**
	 * 分页获取数据字典子项对象列表
	 */
	public String itemGridList() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		sysCodeService.searchItem(page, getFormForSearch());
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 新增数据字典子项对象
	 */
	public String itemGridCreate() throws Exception {
		List<SysCodeForm> list = persistChangeItemForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridCreateMessage(list), list, false);
		return null;
	}
	
	/**
	 * 更新数据字典子项对象
	 */
	public String itemGridUpdate() throws Exception {
		List<SysCodeForm> list = persistChangeItemForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridUpdateMessage(list), list, true);
		return null;
	}
	
	/**
	 * 删除数据字典子项对象
	 */
	public String itemGridDelete() throws Exception {
		Long[] ids = result.getDeletedIds();
		List<String> typeCodeList = sysCodeService.deleteItem(ids);
		
		//同步全局缓存
		WebContextUtils.syncSysCodeBuffer(typeCodeList, this.getApplication());
		
		this.extRenderEditorGridResult(true, ExtEditorGridResult.getEditorGridDeleteMessage(ids));
		return null;
	}
	
	/**
	 * 保存新增或修改的数据字典子项对象
	 */
	private List<SysCodeForm> persistChangeItemForms() throws Exception{
		List<SysCodeForm> list = result.getChangedForms(SysCodeForm.class);
		sysCodeService.saveItem(list);
		
		//同步全局缓存
		WebContextUtils.syncSysCodeBuffer(SysCodeUtils.convertToTypeCodeList(list), this.getApplication());
		
		return list;
	}	
	
	/**
	 * 获取指定数据字典类型的子项列表
	 */
	public String getItems() throws Exception {
		List<SysCodeForm> list = this.getSysCodeItemsWithBuffer(getFormForSearch().getTypeCode(), sysCodeService);
		List<BasicCombo> comboList = SysCodeConverter.convertToBasicCombo(list, true);
		this.extRenderListJson(comboList, true);
		return null;
	}
	
	/**
	 * 获取查询条件值
	 */
	private SysCodeForm getFormForSearch(){
		SysCodeForm form = new SysCodeForm();
		form.setTypeCode(this.getRequest().getParameter("typeCode"));
		form.setTypeName(this.getRequest().getParameter("typeName"));
		form.setItemCode(this.getRequest().getParameter("itemCode"));
		form.setItemName(this.getRequest().getParameter("itemName"));
		return form;
	}
	
	/**
	 * 模型访问方法，使得模型数据自动与页面字段进行关联绑定
	 */
	public ExtEditorGridResult<SysCodeForm> getModel() {
		return result;
	}

	protected void prepareModel() throws Exception {
		result = new ExtEditorGridResult<SysCodeForm>();
	}

	public void prepare() throws Exception {
		prepareModel();
	}
	
	// 其他属性访问函数
	public Page<SysCodeForm> getPage() {
		return page;
	}

	public void setPage(Page<SysCodeForm> page) {
		this.page = page;
	}
}
